home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!news
- From: watzka@stat.uni-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: Help: 2 short functions
- Date: 31 Mar 1996 14:06:01 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4jm3g9$b70@sparcserver.lrz-muenchen.de>
- References: <4ji734$n6v@masala.cc.uh.edu>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- st7jr@Rosie.UH.EDU (odie garfield) writes:
-
- >Hi, I have two functions that I'm not too sure about.
- >The first one:
-
- >char *get_filename(void)
- >{
- > char string[20],*stringp;
- > scanf("%s",string);
- > stringp=&string[0];
- > return stringp;
- >}
- >I'm want the function to read in a string and return a pointer to that string.
- >Am I doing it correctly?
-
- No, you're not.
-
- 1) scanf is not a good method to get input from anything but a
- well-known source.
- 2) "string" _is_ "&string[0]" in an expression context.
- 3) You are returning a pointer to a local variable
-
- >My second function is related to the first function:
-
- >FILE *open_file(char *filenamep)
- >{
- > FILE *fp;
- > fp=fopen("??????","r");
- > return fp;
- >}
- >In this 2nd function, I want to open a file using the pointer to a string I
- >got from the first function. But I don't know exactly how to do this. Any help
- >would be appreciated. Thanks.
-
- Have you tried "fp = fopen(filenamep, "r");"?
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
-